package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.hzdesk.media.matvector.koreabatch.client.my_paperless.config.RBConfig;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.IAddConfigFileContract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.modle.AddConfigFileModle;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.modle.ManageProfilesPagerModle;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.JsonUitl;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/8/21.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class AddConfigFilePersenter implements IAddConfigFileContract.IAddConfigFilePersenter {
    private IAddConfigFileContract.IAddConfigFileView mIAddConfigFileView;
    private IAddConfigFileContract.IAddConfigFileModle mIAddConfigFileModle;
    private Handler handler = new Handler();


    public AddConfigFilePersenter(IAddConfigFileContract.IAddConfigFileView mIAddConfigFileView) {
        this.mIAddConfigFileView = mIAddConfigFileView;
        mIAddConfigFileModle = new AddConfigFileModle();
    }

    @Override
    public void saveConfigFileName(Context context,String devicesId) {
        String inputFileNmae = mIAddConfigFileView.getInputFileName();
        String inputMetting = mIAddConfigFileView.getInputMettingName();
        if(inputFileNmae != null && !TextUtils.isEmpty(inputFileNmae)){
            if(inputMetting != null && !TextUtils.isEmpty(inputMetting)){
                // todo 提交
                submitSave(devicesId,context,inputFileNmae,inputMetting);
            }else{
                mIAddConfigFileView.showMessage("请输入会议名称");
            }
        }else{
            mIAddConfigFileView.showMessage("请输入配置文件名称");
        }
    }
    /**
     * @desc 添加数据
     * */
    private void submitSave(String devicesId, Context context, String inputFileNmae, String inputMetting) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.SAVE_DEVICS_CONFIG_INFO_FILE)
                .addHeader("sessionId",mIAddConfigFileModle.getToken(context))
                .addParams("deviceId", devicesId+"")
                .addParams("meetName", inputMetting+"")
                .addParams("name", inputFileNmae+"")
                .addParams("id", "")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        // Log.e("AddConfigFilePersenter", "loadRecyclerViewDate:token =====> "+ response.body().string());
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                final String normalDate = JsonUitl.getStringDateFeld(object);
                                if(codo == 100) {
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            mIAddConfigFileView.finshPager();
                                        }
                                    });
                                }
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        mIAddConfigFileView.showMessage(normalDate);
                                    }
                                });
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                mIAddConfigFileView.showMessage("保存设备配置失败！");
                            }
                        });
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    @Override
    public void initTtile() {
        mIAddConfigFileView.showTitle();
    }
}
